﻿using CinemaMaestro.BusinessLayer.Contracts;
using CinemaMaestro.DataLayer.Contracts;
using CinemaMaestro.DataLayer.Contracts.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CinemaMaestro.BusinessLayer
{
    public class Sales : BusinessObjectBase, ISales
    {
        public Sales(IUnitOfWork uow)
            : base(uow)
        { }

        public bool Create(Sale sale, int reservationId)
        {
            using(var transaction = _uow.BeginTransaction())
            {
                var screening = sale.Screening;
                Reservation reservation = null;

                if (reservationId != 0)
                {
                    reservation = _uow.ReservationRepository.Read().First(x => x.Id == reservationId);
                }

                string occupiedSeats = String.Empty;

                var reservations = _uow.ReservationRepository.Read()
                    .Where(x => x.Screening.Id == screening.Id);
                foreach (var reserv in reservations)
                {
                    if (reserv.Id != reservationId)
                    {
                        occupiedSeats += reserv.ReservatedSeats;  //1_2,1_3,...
                        occupiedSeats += ",";
                    }                   
                }

                var sales = _uow.SaleRepository.Read()
                    .Where(x => x.Screening.Id == screening.Id);
                foreach (var s in sales)
                {
                    occupiedSeats += s.Seats;  //1_2,1_3,...
                    occupiedSeats += ",";
                }
                if (occupiedSeats != String.Empty)
                {
                    occupiedSeats = occupiedSeats.Substring(0, occupiedSeats.Length - 1);
                }

                bool taken = false;
                if (occupiedSeats != String.Empty)
                {
                    string[] occupiedPositions = occupiedSeats.Split(',');
                    string[] selectedSeats = sale.Seats.Split(',');
                    var takenPositions = occupiedPositions.Intersect(selectedSeats);
                    if (takenPositions.Count() > 0)
                    {
                        return false;
                    }
                }

                var createdSale = _uow.SaleRepository.Create(sale);
                if (reservation != null)
                {
                    _uow.ReservationRepository.Delete(reservationId);
                }
                _uow.SaveChanges();
                transaction.Commit();
                return true;
            }  
        }

        public bool Delete(int id)
        {
            var deleted = _uow.SaleRepository.Delete(id);
            _uow.SaveChanges();
            return deleted;
        }

        public IQueryable<Sale> Read()
        {
            return _uow.SaleRepository.Read();
        }

        public void Update(Sale entity)
        {
            _uow.SaleRepository.Update(entity);
            _uow.SaveChanges();
        }

        public bool SalesHasRelationWithScreening(int screeningId)
        {
            bool hasRelation = Read().Any(x => x.Screening.Id == screeningId);
            return hasRelation;
        }
    }
}
